package org.malred.hotelsystem.fragment.admin;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.adapter.OrderRecycleAdapter;
import org.malred.hotelsystem.dao.OrderDao;
import org.malred.hotelsystem.model.entity.OrderEntity;
import org.malred.hotelsystem.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ManageOrderPageFragment extends Fragment {
    private static final String TAG = "ManageOrderPageFragment";
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private TextView textView;

    RecyclerView rv;
    private List<OrderEntity> orderEntities;
    private OrderRecycleAdapter adapter;

    public ManageOrderPageFragment() {
    }

    public static ManageOrderPageFragment newInstance(String param1) {
        ManageOrderPageFragment fragment = new ManageOrderPageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_order_page, container, false);
        textView = rootView.findViewById(R.id.tv);
        rv = rootView.findViewById(R.id.manage_order_rv);

        List<OrderEntity> items = OrderDao.getAll(new DBUtil(getContext()).getReadableDatabase());
        Log.i(TAG, "onCreateView: "+items);
        orderEntities = new ArrayList<>();

        Log.i(TAG, "onCreateView: " + mParam1);
        if (("" + Integer.parseInt(mParam1)).equals("0")) {
            textView.setText("已预订");

            for (OrderEntity orderEntity : items) {
                if (Objects.equals(orderEntity.getStatus(), "0")) {
                    orderEntities.add(orderEntity);
                }
            }
        }
        if (("" + Integer.parseInt(mParam1)).equals("1")) {
            textView.setText("已入住");

            for (OrderEntity orderEntity : items) {
                if (Objects.equals(orderEntity.getStatus(), "1")) {
                    orderEntities.add(orderEntity);
                }
            }
        }
        if (("" + Integer.parseInt(mParam1)).equals("2")) {
            textView.setText("已退订");

            for (OrderEntity orderEntity : items) {
                if (Objects.equals(orderEntity.getStatus(), "2")) {
                    orderEntities.add(orderEntity);
                }
            }
        }

        adapter = new OrderRecycleAdapter(getContext(), orderEntities);
        rv.setAdapter(adapter);
        // recycle view 显示方式(布局)
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(manager);

        return rootView;
    }
}
